package com.bdqn;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class ShortAnswer2_view {
    public static void main(String[] args) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse("src/main/java/com/bdqn/Student.xml");

            NodeList scores = document.getElementsByTagName("student");
            
            // 循环输出学生信息
            for (int i = 0; i < scores.getLength() ; i++) {
                
                Node item = scores.item(i);
                Element element = (Element) item;
                String id = element.getAttribute("id");
                System.out.println("学生id:" + id);
                NodeList childNodes = element.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    
                    Node item1 = childNodes.item(j);
                    
                    if (item1.getNodeType() == Node.ELEMENT_NODE){
                        Element element1 = (Element) item1;
                        System.out.println(element1.getTextContent());
                    }
                    
                }
                System.out.println("\n");

                }



        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
